import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/pages/mine/user_manage/mine_add_user/views/add_user_bottom.dart';
import 'package:yt_devops/pages/mine/user_manage/user_manager_user_detail/user_avatar_widget.dart';
import 'package:yt_devops/pages/site/site_setting/site_setting_base_info/widgets/item_widget.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/brn_row1_delegate.dart';
import 'user_manager_user_detail_controller.dart';

class UserManagerUserDetailPage
    extends GetView<UserManagerUserDetailController> {
  const UserManagerUserDetailPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<UserManagerUserDetailController>(
      builder: (_) {
        return Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBarWidget.withRightActions(
            ResKey.mineUserManagerUserDetail.tr,
            actions: [
              Visibility(
                visible: controller.editAuth && !controller.isEnableEdit,
                child: IconButton(
                  icon: Image.asset(ImageAssets.siteSettingBaseInfoEdit),
                  onPressed: controller.edit,
                ),
              )
            ],
          ),
          body: Column(
            children: [
              Expanded(
                  child: ListView(
                children: [
                  Gaps.vGap10,
                  _topWidget(),
                  Gaps.vGap10,
                  _middleWidget(),
                  // Gaps.vGap10,
                  AddUserBottom(
                      controller.pushTypeList,
                      controller.pushLevelList,
                      controller.remarkController,
                          (List list) {
                        controller.pushTypeList = list;
                        controller.checkCanCommit();
                      },

                          (List list) {
                        controller.pushLevelList = list;
                        controller.checkCanCommit();
                      }, (String ? remark)  {
                    controller.remarkController.text = remark??'';
                    controller.checkCanCommit();
                    controller.update();
                  },isCanEdit: controller.isEnableEdit,),
                ],
              )),
              // const Spacer(),
              Visibility(
                  visible: controller.isEnableEdit,
                  child: Align(
                    alignment: Alignment.bottomCenter,
                    child: Container(
                      color: Colors.white,
                      height: 82 + ScreenUtil().bottomBarHeight,
                      padding:
                          EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
                      child: YtBrnBottomButtonPanel(
                          enableMainButton: controller.enableCommit,
                          mainButtonName: ResKey.save.tr,
                          mainButtonOnTap: () => controller.save(),
                          secondaryButtonName: ResKey.cancel.tr,
                          enableSecondaryButton: true,
                          secondaryButtonBorder:
                              Border.all(color: Colour.dividerColor),
                          secondaryButtonOnTap: () => controller.cancel()),
                    ),
                  )),
            ],
          ),
        );
      },
    );
  }

  _topWidget() {
    return Container(
      clipBehavior: Clip.hardEdge,
      margin: const EdgeInsets.only(left: 16,right: 16),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        color: Colors.white,
      ),
      height: 76 + 50,
      child: Column(
        children: [
          UserAvatarWidget(controller.avatar, (avatar) {
            controller.avatar = avatar;
            controller.update();
          },
              isCanClick: controller.isEnableEdit,
              isShowRightArrow: controller.isEnableEdit),
          Container(
            height: 1,
            color: Colour.dividerColor,
          ),
          ItemWidget(
            ResKey.mineUserManagerUserStatus.tr,
            controller.statusController,
            isShowRight: controller.isEnableEdit,
            isCanEdit: false,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isRequired: controller.isEnableEdit,
            hintString: ResKey.pleSelect.tr,
            clickCallBack: () {
              if (controller.isEnableEdit) {
                showBottomPop(controller.statusList);
              }
            },
          ),
        ],
      ),
    );
  }

  _middleWidget() {
    return Container(
      margin: const EdgeInsets.only(left: 16,right: 16),
        clipBehavior: Clip.hardEdge,
      decoration: const BoxDecoration(
        borderRadius: BorderRadius.only(topLeft: Radius.circular(8),topRight: Radius.circular(8),),
        color: Colors.white,
      ),
      child: Column(
        children: [
          ItemWidget(
            ResKey.mineUserManagerUserName.tr,
            controller.userNameController,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isCanEdit: controller.isEnableEdit,
            isRequired: controller.isEnableEdit,
            textChangeCallBack: (text) {
              controller.checkCanCommit();
            },
          ),
          ItemWidget(
            ResKey.mineUserManagerRole.tr,
            controller.roleController,
            isShowRight: controller.isEnableEdit,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            hintString: ResKey.pleSelect.tr,
            isCanEdit: false,
            isRequired: controller.isEnableEdit,
            isShowDivider: false,
            clickCallBack: () {
              if (controller.isEnableEdit) {
                controller.chooseRole();
              }
            },
          ),
          ItemWidget(
            ResKey.mineUserManagerOrganization.tr,
            controller.organizationController,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isShowRight: controller.isEnableEdit,
            isCanEdit: false,
            isRequired: controller.isEnableEdit,
            hintString: ResKey.pleSelect.tr,
            clickCallBack: () {
              if (controller.isEnableEdit) {
                controller.chooseOrganization();
              }
            },
          ),
          ItemWidget(
            ResKey.mineUserManagerMobile.tr,
            controller.mobileController,
            titleColor: Colour.mainTextColor,
            titleFont: 16,
            isCanEdit: controller.isEnableEdit,
            isRequired: false,
            textChangeCallBack: (text) {
              controller.checkCanCommit();
            },
          ),
          ItemWidget(
              ResKey.mineUserManagerUserEmail.tr, controller.emailController,
              titleColor: Colour.mainTextColor,
              titleFont: 16,
              isShowDivider: ['3', '4'].contains(controller.selectedRoleId),
              isCanEdit: controller.isEnableEdit),
          Visibility(
              visible: !controller.systemBuiltRoles
                  .contains(controller.selectedRoleId),
              // visible:true,
              child: ItemWidget(
                ResKey.mineUserManagerUserRelationSite.tr,
                titleColor: Colour.mainTextColor,
                titleFont: 16,
                controller.relationSiteController,
                isShowRight: controller.isEnableEdit,
                isCanEdit: false,
                isRequired: true,
                hintString: ResKey.pleSelect.tr,
                isShowDivider: false,
                clickCallBack: () {
                  if (controller.isEnableEdit) {
                    controller.chooseRelateSite();
                  }
                },
              )),
        ],
      ),
    );
  }

  ///弹出底部弹窗
  showBottomPop(List<String> list) {
    BrnMultiDataPicker(
      context: Get.context!,
      title: ResKey.mineUserManagerUserStatus.tr,
      confirmClick: (indexList) {
        controller.statusController.text = list[indexList[0]].toString();
        controller.selectedStatusIndex = indexList[0];
        controller.checkCanCommit();
      },
      delegate: Brn1RowDelegate(list,
          firstSelectedIndex: controller.selectedStatusIndex),
    ).show();
  }
}


// pickerTitleConfig: BrnPickerTitleConfig(
//         titleContent: pickerTitle,
//         cancel: Text(
//           ResKey.cancel.tr,
//           style: TextStyle(
//             color: Colour.lightTextColor,
//             fontSize: 14.sp,
//           ),
//         ),
//         confirm: Text(
//           ResKey.determine.tr,
//           style: TextStyle(
//             color: Colour.primaryColor,
//             fontSize: 14.sp,
//           ),
//         ),
//       ),